package com.deyuanyun.pic.settings.service.msgtemplate.pbf;

import com.deyuanyun.pic.common.spring.SpringAppliction;
import com.deyuanyun.pic.common.util.CommonUtil;
import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.settings.controller.dto.BizMsgThresholdVO;
import com.deyuanyun.pic.settings.controller.dto.base.DefectInfoDTO;
import com.deyuanyun.pic.settings.controller.pipe.vo.PipeLocationDTO;
import com.deyuanyun.pic.settings.domain.base.BizMsgUser;
import com.deyuanyun.pic.settings.domain.pipe.PipeLine;
import com.deyuanyun.pic.settings.service.msgtemplate.AbstractPushMsgTemplate;
import com.deyuanyun.pic.settings.service.msgtemplate.MsgQueue;
import com.deyuanyun.pic.settings.service.pipe.PipeLocationService;
import com.deyuanyun.pic.settings.support.utils.PipeDictUtils;
import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 *  缺陷通知消息推送
 *  @author axiao(xj08.dream@gmail.com)
 *  @date 2016-10-12
 *  @version 1.0
 *  Histroy: [修改人][时间][描述]
 */
public class DefectNoticePushMsg extends AbstractPushMsgTemplate {

    private static final Logger LOGGER = LoggerFactory.getLogger(DefectNoticePushMsg.class);

    public static final String DEFECT_INFO_DTO = "defectInfoDTO";

    public static final String DEFECT_NOTICE = "defectNotice";

    private String createRemark = "提醒开展管体缺陷修复工作消息推送";

    @Override
    public void doPushMsgData() {
        if(super.interfaceParam.isEmpty()) {
            LOGGER.info("没有传入参数");
            return;
        }
        Map<String, Object> interfaceMap = super.interfaceParam;
        Object defectInfoObj = interfaceMap.get(DEFECT_INFO_DTO); //取出缺陷实体类信息
        Object defectNoticeObj = interfaceMap.get(DEFECT_NOTICE); //取出缺陷通知业务阀值实体类信息
        if(defectInfoObj != null && defectNoticeObj != null) {
            initializeBizMsgSends(defectInfoObj, defectNoticeObj);
        }
    }

    @Override
    public void initializeBizMsgSends(Object... args) {
        DefectInfoDTO defectInfoDTO = (DefectInfoDTO) args[0];
        BizMsgThresholdVO defectNotice = (BizMsgThresholdVO) args[1];
        String pipeLineId = defectInfoDTO.getPipeLineId();
        LOGGER.info("已经得到需要通知的缺陷信息和发送消息必须的业务阀值设置信息");
        //提醒指定角色开展管体缺陷修复工作

        PipeLocationService pipeLocationService = SpringAppliction.getBean(PipeLocationService.class);

        PipeLine pipeLine = PipeDictUtils.getLineById(defectInfoDTO.getPipeLineId());
        PipeLocationDTO locationFrom = pipeLocationService.queryPipeLocationById(pipeLineId, pipeLine.getFromPipeValveroomId());
        PipeLocationDTO locationTo = pipeLocationService.queryPipeLocationById(pipeLineId, pipeLine.getToPipeValveroomId());

        if(pipeLine != null && locationFrom != null && locationTo != null) {
            super.thresListCastString(Lists.newArrayList(defectInfoDTO.getPipeLineName(), locationFrom.getName(), locationTo.getName()));
        }

        List<String> roleIds = defectNotice.getRoleIdList(); //角色id集合
        if(roleIds != null && !roleIds.isEmpty()) {
            Set<String> userIds = super.bizMsgThresholdService.queryUseIdByRoleIds(roleIds); //用户id集合
            List<MsgQueue.BizMsgUserInfo> userInfos = new ArrayList<MsgQueue.BizMsgUserInfo>();
            if (userIds != null && !userIds.isEmpty()) {
                for (String userId : userIds) {
                    LOGGER.info("开始向用户id为：" + userId + "的用户推送消息");
                    BizMsgUser bizMsgUser = new BizMsgUser();
                    if (StringUtils.isNotEmpty(super.thresholdContent)) {
                        bizMsgUser.setThresholdContent(super.thresholdContent);
                    }
                    bizMsgUser.setId(CommonUtil.getUUID());
                    bizMsgUser.setMsgContent(defectNotice.getMsgContent());
                    bizMsgUser.setUserId(userId);
                    bizMsgUser.setCreateDate(new Date());
                    bizMsgUser.setCreateRemark(createRemark);

                    userInfos.add(msgQueue.new BizMsgUserInfo().setValid(false).setBizMsgUser(bizMsgUser));
                }

                if(!userInfos.isEmpty()) {
                    MsgQueue.MsgInfo msgInfo = msgQueue.new MsgInfo().setBizMsgThresholdId(defectNotice.getId())
                            .setThresholdSet(defectNotice.getThresholdContent()).setRoleIds(roleIds);
                    msgQueue.addOperator(msgQueue.new PushMsgOperator(userInfos, msgInfo));
                }
            }
        }
    }

    @Override
    public boolean valiIsPush(Map<String, Object> valiMap) {
        return false;
    }
}
